/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.json.database;

import com.json.bean.Admin;
import com.json.bean.User;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * 数据库更高一级更改
 *
 * @author json
 */
public class AdminDao {

    /**
     *
     * 插入一个作业
     *
     * @param homework 用户的POJO类 ok
     * @return 是否成功
     */
    public boolean insert(Admin homework) {
        // String sql = "insert into homework (work,beizhu) values('shinima','nima')";
        String sql = "insert into homework (work,beizhu) values("
                + "'" + homework.getUser() + "'"
                + "," + "'" + homework.getPassword() + "'"
                + ")";

        DatabaseAccess1 db = new DatabaseAccess1();
        boolean b = db.update(sql);//增
        db.close();
        return b;
    }

    /**
     * 删除某一项
     *
     * @param id
     * @return 如果成功返回true 否则返回false ok
     */
    public boolean dell(int id) {
        String sql = "delete from homework where id=" + id;
        DatabaseAccess1 db = new DatabaseAccess1();
        boolean b = db.update(sql);//删
        db.close();
        return b;
    }

    /**
     *
     * 修改提交的作业
     *
     *
     * @param homework    ok
     * @return
     */
    public boolean update(Admin homework) {
        String sql = "update homework set "
                + "work=" + "'" + homework.getUser() + "'" + ","
                + "beizhu=" + "'" + homework.getPassword() + "'"
                + " where id="  + homework.getId();
        DatabaseAccess1 db = new DatabaseAccess1();
        boolean b = db.update(sql);//修
        db.close();
        return b;

    }

    /**
     *
     *
     *
     *
     * 根据用户名 获得一个用户
     *
     * @param username 用户名
     * @return 用户的POJO类
     */
    public User getOneUser(String username) {
        String sql = "select * from user where username='" + username + "'";
        DatabaseAccess db = new DatabaseAccess();
        ResultSet rs = db.query(sql);
        User user = new User();
        try {
            if (rs != null && rs.next()) {
                user.setId(rs.getInt("id"));
                user.setUsername(rs.getString("username"));
                user.setPass(rs.getString("pass"));
                // user.setAdm(rs.getInt("adm"));
                //  user.setAge(rs.getInt("age"));
            }
        } catch (SQLException ex) {
            Logger.getLogger(UserDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        db.close();
        return user;

    }

    /**
     * 根据用户id获得一个用户
     *
     * @param id 用户id
     * @return 用户的POJO类
     */
    public User getOneUser(int id) {
        String sql = "select * from user where id=" + id + "";
        DatabaseAccess db = new DatabaseAccess();
        ResultSet rs = db.query(sql);
        User user = new User();
        try {
            if (rs != null && rs.next()) {
                user.setUsername(rs.getString("username"));
                user.setPass(rs.getString("pass"));
                // user.setAdm(rs.getInt("adm"));
                //   user.setAge(rs.getInt("age"));
                user.setId(rs.getInt("id"));
            }
        } catch (SQLException ex) {
            Logger.getLogger(UserDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        db.close();
        return user;
    }

    /**
     *
     * 获得所有用户
     *
     * @return 用户集合
     */
    public LinkedList<User> getUserLisk() {
        String sql = "select * from user";
        DatabaseAccess db = new DatabaseAccess();
        ResultSet rs = db.query(sql);
        LinkedList<User> userlist = new LinkedList<>();
        try {
            while (rs != null && rs.next()) {
                User user = new User();
                user.setId(rs.getInt("id"));
                user.setUsername(rs.getString("username"));
                user.setPass(rs.getString("pass"));
                //   user.setAdm(rs.getInt("adm"));
                // user.setAge(rs.getInt("age"));
                userlist.add(user);
            }
        } catch (SQLException ex) {
            Logger.getLogger(UserDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        db.close();
        return userlist;
    }

    /**
     *
     *
     *
     *
     * 验证登陆
     *
     * @param admin
     * @return 登陆成功返回true 其他返回false
     */
    public boolean login(Admin admin) {
        String sql = "select * from admin where user='" + admin.getUser() + "' and password='" + admin.getPassword() + "'";//这里不要写&&  而是and
        DatabaseAccess1 db = new DatabaseAccess1();
        db.query(sql);
        boolean b = db.isResultSetNull();
        db.close();
        return b;
    }

    public boolean check(User user) {
        String sql = "select * from user where username='" + user.getUsername() + "'";
        DatabaseAccess db = new DatabaseAccess();
        db.query(sql);
        boolean b = db.isResultSetNull();
        db.close();
        return b;

    }
}
